<?Lassoscript
// Last modified 4/17/09 by Eric Landmann

// CHANGE NOTES
// 4/17/09
// Version 1.0

Include:'siteconfig.lasso';

Var:'vPage_Title'='Search the Forums';

Include:($svLibsPath)'page_header.inc';

// Debugging
// Var:'svDebug' = 'Y';

// Start the Session
Include:($svLibsPath)'sessions.inc';
Include:($svLibsPath)'header.inc';

//	Defining svVars
If: !(Var_Defined:'SQLQueryPosts'); Session_AddVariable: -Name=$svSessionName, 'SQLQueryPosts'; /If;
If: !(Var_Defined:'svSearchParams'); Session_AddVariable: -Name=$svSessionName, 'svSearchParams'; /If;
If: !(Var_Defined:'svSearchParamsURL'); Session_AddVariable: -Name=$svSessionName, 'svSearchParamsURL'; /If;


// Setting Variables for Nav Control
Var:'vNew' = (Action_Param:'New');
Var:'Records_Skipped' = (Action_Param:'skip');
Var:'This_Group' = (Action_Param:'group');
Var:'Records_Per_Page' = 10;
Var:'SkipCount' = integer;
Var:'Records_Found' = integer;
Var:'Group_Size' = (Math_Mult:(Var:'Records_Per_Page'),5);
	
If: $svDebug == 'Y';
	'<br>\n';
	'<strong>searchresponse.lasso</strong><br>\n';
/If;

// If a new query, initialize all the variables
If: Var:'vNew' == 'Y';
	Var:'vID' = (Action_Param:'ID');
	Var:'vTopic' = (Action_Param:'Topic');
	Var:'vSID' = (Action_Param:'SID');
	Var:'vPoster' = (Action_Param:'Poster');
	Var:'vMessage' = (Action_Param:'Message');

	Var:'SQLPostID' = string;
	Var:'SQLTopic' = string;
	Var:'SQLSection' = string;
	Var:'SQLPoster' = string;
	Var:'SQLMessage' = string;

	Var:'SQLQueryPosts' = '';
	Var:'SQLQuerySuffix' = '';
	Var:'svSearchParams' = '';
	Var:'svSearchParamsURL' = '';

	// Include the code for building the search params phrase
	Include:($svLibsPath) 'searchparamsposts_build.inc';

/If;

// SAMPLE QUERY
/* 
/* Selecting posts where Section_ID is defined */
/* and Topic_Title LIKE */
/* SELECT DISTINCT
	(t.Topic_ID), p.ID,
p.Poster, p.Message, p.PostDate,
	p.PostTime, p.Smiley, p.Allow
FROM forums_posts p, forums_topics t
INNER JOIN forums_topics
	ON  t.Topic_ID =  p.Topic_ID
WHERE
	p.ID = "57"
	AND t.Section_ID = "2"
	AND t.Topic_Title LIKE "%Topic 1%"
	AND (p.allow = "Y" OR p.allow="A") */

// Define the suffix (same for any query)
Var:'SQLQuerySuffix' = ' AND (p.allow = "Y" OR p.allow="A") ORDER BY t.Topic_Title';

If: Var:'SQLQueryPosts' == '';
	Var:'SQLQueryPosts' = '/* 83 searchresponse - $SQLQueryPosts */
SELECT p.ID, p.Topic_ID, p.Poster, p.Message, p.PostDate, p.PostTime, p.Smiley, p.Allow
FROM ' $svPostsTable ' AS p
INNER JOIN ' $svTopicsTable ' AS T ON (t.Topic_ID =  p.Topic_ID)
WHERE ';
	
	// POST ID SEARCH
	If: Var:'vID' != '';
		Var:'SQLPostID' = 'p.ID = "' $vID;
		$SQLQueryPosts += $SQLPostID '"';
		If: Var:'vTopic' != '' || Var:'vSID' != '' || Var:'vPoster' != '' || Var:'vMessage' != '';
			$SQLQueryPosts += ' AND ';
		/If;
	/If;
	
	// TOPIC SEARCH
	If: Var:'vTopic' != '';
		Var:'SQLTopic' = 't.Topic_Title LIKE "%' $vTopic;
		$SQLQueryPosts += $SQLTopic '%"';
		If: Var:'vSID' != '' || Var:'vPoster' != '' || Var:'vMessage' != '';
			$SQLQueryPosts += ' AND ';
		/If;
	/If;

	// SECTION SEARCH
	If: Var:'vSID' != '';
		Var:'SQLSection' = 't.Section_ID = "' $vSID;
		$SQLQueryPosts += $SQLSection '"';
		If:  Var:'vPoster' != '' || Var:'vMessage' != '';
			$SQLQueryPosts += ' AND ';
		/If;
	/If;

	// POSTER SEARCH
	If: Var:'vPoster' != '';
		Var:'SQLPoster' = 'p.Poster LIKE "%' $vPoster;
		$SQLQueryPosts += $SQLPoster '%"';
		If: Var:'vMessage' != '';
			$SQLQueryPosts += ' AND ';
		/If;
	/If;

	// MESSAGE SEARCH
	If: Var:'vMessage' != '';
		Var:'SQLMessage' = 'p.Message LIKE "%' $vMessage;
		$SQLQueryPosts += $SQLMessage '%"';
	/If;

	// Add suffix
	$SQLQueryPosts += $SQLQuerySuffix;


	// SEARCHES EVERYTHING
	// If no search params passed, redefine the query to get everything, including Inactive
	If: ($SQLPostID == '') && ($SQLTopic == '') && ($SQLSection == '') && ($SQLPoster == '') && ($SQLMessage == '');
		Var:'SQLQueryPosts' = '/* 149 searchresponse - $SQLQueryPosts */
SELECT DISTINCT (t.Topic_ID), p.ID,
p.Poster, p.Message, p.PostDate, p.PostTime, p.Smiley, p.Allow
/* FROM ' $svPostsTable ' AS p, ' $svTopicsTable ' AS t */
FROM ' $svTopicsTable ' AS t
INNER JOIN ' $svPostsTable ' AS p ON (t.Topic_ID = p.Topic_ID)';
	/If;

	Debug;
		'148: vID = ' (Var:'vID') '<br>\n';
		'148: vTopic = ' (Var:'vTopic') '<br>\n';
		'148: vSID = ' (Var:'vSID') '<br>\n';
		'148: vPoster = ' (Var:'vPoster') '<br>\n';
		'148: vMessage = ' (Var:'vMessage') '<br>\n';
		'148: SQLQueryPosts = ' (Var:'SQLQueryPosts') '<br>\n';
		'148: svSearchParams = ' (Var:'svSearchParams') '<br>\n';
		'148: svSearchParamsURL = ' (Var:'svSearchParamsURL') '<br>\n';
	/Debug;

/If;

Inline: $IV_SearchPosts,
	-SkipRecords=(Var:'Records_Skipped'),
	-MaxRecords=(Var:'Records_Per_Page'),
	-SQL=$SQLQueryPosts;

	Debug;
		'168: Error_CurrentError = ' (Error_CurrentError) '<br>\n';
		'168: Found_Count = ' (Found_Count) '<br>\n';
	/Debug;

	// If no records found, immediate redirect with error to search.lasso
	If: (Found_Count) == 0;
		Var:'vError'='2000';
		Var:'URL' = ('search.lasso?Error='$vError'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'192: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;

	// If only one record, redirect immediately to thread.lasso
	// This shortcut allows users to skip past the search results page if there is only one record found
	// Example URL:
	// http://127.0.0.1/thread.lasso?ID=57&New=Y
	If: (Var:'vID') != '' && (Found_Count) == 1;
//		Var:'vError'='2000';
		Var:'URL' = ('thread_noedit.lasso?ID='($vID));
		If: $svDebug == 'Y';
			Debug;
				'192: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;

?><table border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="black">
	<tr> 
		<td width="100%" valign="top" bgcolor="#FFFFFF">
			<div align="left">
		<h2>Search Results</h2>
			<div align="left"><font color="red">[Var:'svSearchParams', -EncodeNone]</font>&nbsp;&nbsp;&nbsp;<a href="search.lasso[$svSearchParamsURL]">Search Again</a></div><br>
		</div>
		<div align="center">
			<table width="100%" cellspacing="0" cellpadding="5">
				<tr bgcolor="#000000">
					<td width="20" class="tablehead_10_white">ID</td>
					<td width="180" class="tablehead_10_white">Topic</td>
					<td width="90" class="tablehead_10_white">Posted By</td>
					<td width="60" class="tablehead_10_white">PostDate</td>
				</tr>     
<?LassoScript
			Records;
				Var:'vID' = (Field:'ID');
				Var:'vTID' = (Field:'Topic_ID');
				Var:'vPoster' = (Field:'Poster');
				Var:'vMessage' = (Field:'Message');

				// Get Topic Title
				Inline: $IV_SearchTopics, -Table=$svTopicsTable,
					'Topic_ID' = (Var:'vTID');
					Var:'vTopic_Title' = (Field:'Topic_Title');
				/Inline;

?>				<tr <?Lassoscript If: (Loop_Count) %2 == 0; 'bgcolor = "'$svPostContentColor'"'; Else; 'bgcolor = "#FFFFFF"'; /If; ?> >
				<td valign="top" width="20" class="tabletext_10_black"><strong><a href="thread_noedit.lasso?ID=[Var:'vID']" target="_blank">[Var:'vID']</a></strong></td>
				<td valign="top" width="300" class="tabletext_10_black"><strong>[$vTopic_Title]</strong></td>
				<td valign="top" width="90" class="tabletext_10_black">[$vPoster]</td>
				<td valign="top" width="60" class="tabletext_10_black">[li_yesterday_hour: -dbDate=(Field:'PostDate')]&nbsp;&nbsp;</td>
			</tr>
				[/Records]
			<tr>
				<td colspan="6" valign="top" bgcolor="[$svTopicFooterColor]">&nbsp;</td>
			</tr>
		</table>
	</div>
<div class="NavLinkContainer">
<?Lassoscript
// Navigation control links
Include:($svLibsPath)'nav_control.inc';
// Output the Nav Control Links
$NavControlLinks;

	/Inline;

?>		</div>
		</td>
	</tr>
</table>
[Include:($svLibsPath) 'footer.inc']
</body>
</html>
